<template>
  <div class="search-bar">
    <!-- 搜索输入框 -->
    <div class="search-container">
      <el-input
        v-model="searchKeyword"
        placeholder="搜索你想要的商品..."
        class="search-input"
        size="large"
        @keyup.enter="handleSearch"
        clearable
      >
        <template #prepend>
          <el-select v-model="searchCategory" placeholder="分类" style="width: 120px">
            <el-option label="全部" value="" />
            <el-option 
              v-for="category in categories" 
              :key="category.id" 
              :label="category.name" 
              :value="category.id.toString()" 
            />
          </el-select>
        </template>
        <template #append>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>
            搜索
          </el-button>
        </template>
      </el-input>
    </div>
    
    <!-- 热门搜索标签 -->
    <div class="hot-keywords">
      <span class="hot-label">热门搜索：</span>
      <el-tag
        v-for="keyword in hotKeywords"
        :key="keyword"
        size="small"
        class="hot-tag"
        @click="searchHotKeyword(keyword)"
      >
        {{ keyword }}
      </el-tag>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { Search } from '@element-plus/icons-vue'
import request from '@/utils/request'
import type { Category } from '@/types'

const router = useRouter()
const searchKeyword = ref('')
const searchCategory = ref('')
const categories = ref<Category[]>([])

// 热门搜索关键词
const hotKeywords = ref([
  'iPhone', 'MacBook', '耐克', '阿迪达斯', 'iPad', 
  '手表', '相机', '游戏机', '书籍', '家具'
])

// 加载分类数据
const loadCategories = async () => {
  try {
    const response = await request.get('/category/list', {
      params: {
        pageSize: 20,
      }
    })
    
    if (response && response.data) {
      const categoryData = Array.isArray(response.data) ? response.data : 
                          (response.data.data && Array.isArray(response.data.data)) ? response.data.data : []
      
      if (categoryData.length > 0) {
        categories.value = categoryData.filter((cat: Category) => cat.isUse)
      }
    }
  } catch (error) {
    console.error('加载分类失败:', error)
    // 使用默认分类
    categories.value = [
      { id: 1, name: '电子产品', isUse: true },
      { id: 2, name: '服装配饰', isUse: true },
      { id: 3, name: '家居用品', isUse: true },
      { id: 4, name: '运动户外', isUse: true },
      { id: 5, name: '图书音像', isUse: true }
    ]
  }
}

// 搜索处理
const handleSearch = () => {
  if (!searchKeyword.value.trim()) return
  
  // 构建查询参数
  const query: any = {
    keyword: searchKeyword.value.trim()
  }
  
  // 如果有选择分类，添加分类参数
  if (searchCategory.value) {
    query.categoryId = searchCategory.value
  }
  
  router.push({
    path: '/products',
    query
  })
}

// 点击热门关键词搜索
const searchHotKeyword = (keyword: string) => {
  searchKeyword.value = keyword
  handleSearch()
}

// 初始化
onMounted(() => {
  loadCategories()
})
</script>

<style scoped>
.search-bar {
  background: linear-gradient(145deg, #ffffff 0%, #fefefe 100%);
  padding: 30px;
  border-radius: 20px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.08);
  border: 1px solid rgba(255, 107, 53, 0.1);
  margin-bottom: 40px;
  position: relative;
  overflow: hidden;
}

.search-bar::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, #ff6b35 0%, #f7931e 100%);
}

.search-container {
  margin-bottom: 20px;
}

.search-input {
  max-width: 700px;
  margin: 0 auto;
  display: block;
}

.search-input {
  max-width: 700px;
  margin: 0 auto;
  display: block;
}

/* 主输入框样式 */
.search-input :deep(.el-input__wrapper) {
  border-radius: 30px;
  padding: 0;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  border: 2px solid transparent;
  transition: all 0.3s ease;
  height: 56px;
  background: #fff;
}

.search-input :deep(.el-input__wrapper:hover) {
  border-color: rgba(255, 107, 53, 0.3);
  box-shadow: 0 6px 20px rgba(255, 107, 53, 0.1);
}

.search-input :deep(.el-input__wrapper.is-focus) {
  border-color: #ff6b35;
  box-shadow: 0 6px 20px rgba(255, 107, 53, 0.2);
}

/* 输入框内部样式 */
.search-input :deep(.el-input__inner) {
  border: none;
  border-radius: 30px;
  padding: 0 20px;
  font-size: 16px;
  height: 56px;
  line-height: 56px;
  background: transparent;
}

/* 前置分类选择器样式 */
.search-input :deep(.el-input-group__prepend) {
  border-radius: 30px 0 0 30px;
  background: linear-gradient(145deg, #f8f9fa 0%, #f1f3f4 100%);
  border: none;
  padding: 0 15px;
  border-right: 1px solid #e9ecef;
  height: 56px;
  display: flex;
  align-items: center;
}

.search-input :deep(.el-input-group__prepend .el-select) {
  width: 110px;
}

.search-input :deep(.el-input-group__prepend .el-select .el-input__wrapper) {
  box-shadow: none;
  border: none;
  background: transparent;
  border-radius: 0;
  padding: 0;
  height: 40px;
}

.search-input :deep(.el-input-group__prepend .el-select .el-input__inner) {
  height: 40px;
  line-height: 40px;
  font-size: 14px;
  color: #606266;
}

/* 后置搜索按钮样式 */
.search-input :deep(.el-input-group__append) {
  border-radius: 0 30px 30px 0;
  border: none;
  padding: 0;
  overflow: hidden;
  height: 56px;
}

.search-input :deep(.el-input-group__append .el-button) {
  border-radius: 0 30px 30px 0;
  background: linear-gradient(135deg, #ff6b35 0%, #f7931e 100%);
  border: none;
  padding: 0 25px;
  height: 56px;
  font-weight: 500;
  font-size: 16px;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 8px;
}

.search-input :deep(.el-input-group__append .el-button:hover) {
  background: linear-gradient(135deg, #f7931e 0%, #ff6b35 100%);
  transform: scale(1.02);
}

.search-input :deep(.el-input-group__append .el-button:active) {
  transform: scale(0.98);
}

.hot-keywords {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 12px;
  max-width: 700px;
  margin: 0 auto;
  padding-top: 5px;
}

.hot-label {
  color: #7f8c8d;
  font-size: 15px;
  font-weight: 500;
  margin-right: 8px;
  flex-shrink: 0;
}

.hot-tag {
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  border: 1px solid #e9ecef;
  background: linear-gradient(145deg, #ffffff 0%, #f8f9fa 100%);
  color: #6c757d;
  border-radius: 20px;
  padding: 8px 16px;
  font-size: 13px;
  font-weight: 500;
  position: relative;
  overflow: hidden;
  height: 32px;
  line-height: 16px;
  display: flex;
  align-items: center;
}

.hot-tag::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 107, 53, 0.1), transparent);
  transition: left 0.5s;
}

.hot-tag:hover::before {
  left: 100%;
}

.hot-tag:hover {
  background: linear-gradient(135deg, #ff6b35 0%, #f7931e 100%);
  color: white;
  border-color: #ff6b35;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(255, 107, 53, 0.3);
}

.hot-tag:active {
  transform: translateY(0);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .search-bar {
    padding: 25px 20px;
    margin-bottom: 30px;
  }
  
  .search-input {
    max-width: 100%;
  }
  
  .search-input :deep(.el-input__wrapper) {
    height: 52px;
  }
  
  .search-input :deep(.el-input__inner) {
    height: 52px;
    line-height: 52px;
    font-size: 15px;
  }
  
  .search-input :deep(.el-input-group__prepend) {
    height: 52px;
    padding: 0 12px;
  }
  
  .search-input :deep(.el-input-group__prepend .el-select) {
    width: 90px;
  }
  
  .search-input :deep(.el-input-group__prepend .el-select .el-input__wrapper) {
    height: 38px;
  }
  
  .search-input :deep(.el-input-group__prepend .el-select .el-input__inner) {
    height: 38px;
    line-height: 38px;
    font-size: 13px;
  }
  
  .search-input :deep(.el-input-group__append) {
    height: 52px;
  }
  
  .search-input :deep(.el-input-group__append .el-button) {
    padding: 0 20px;
    height: 52px;
    font-size: 15px;
  }
  
  .hot-keywords {
    max-width: 100%;
  }
  
  .hot-label {
    width: 100%;
    text-align: center;
    margin-bottom: 10px;
    margin-right: 0;
  }
}

@media (max-width: 480px) {
  .search-bar {
    padding: 20px 15px;
  }
  
  .search-input :deep(.el-input__wrapper) {
    height: 48px;
  }
  
  .search-input :deep(.el-input__inner) {
    font-size: 14px;
    height: 48px;
    line-height: 48px;
    padding: 0 15px;
  }
  
  .search-input :deep(.el-input-group__prepend) {
    height: 48px;
    padding: 0 10px;
  }
  
  .search-input :deep(.el-input-group__prepend .el-select) {
    width: 80px;
  }
  
  .search-input :deep(.el-input-group__prepend .el-select .el-input__wrapper) {
    height: 36px;
  }
  
  .search-input :deep(.el-input-group__prepend .el-select .el-input__inner) {
    height: 36px;
    line-height: 36px;
    font-size: 12px;
  }
  
  .search-input :deep(.el-input-group__append) {
    height: 48px;
  }
  
  .search-input :deep(.el-input-group__append .el-button) {
    padding: 0 15px;
    height: 48px;
    font-size: 14px;
    gap: 6px;
  }
  
  .hot-tag {
    font-size: 12px;
    padding: 6px 14px;
    height: 28px;
  }
}
</style>